package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1155 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public Map<String, Integer> memo = new HashMap<>();
        public static final int factor = 1000000000 + 7;

        public int numRollsToTarget(int d, int f, int target) {
            return dfs(d, f, target);
        }

        // d 个一样的骰子，每个骰子上都有 f 个面，分别标号为 1, 2, ..., f
        // 模 10^9 + 7 后返回
        public int dfs(int d, int f, int target) {
            if (d <= 0) return target == 0 ? 1 : 0;
            String key = d + "_" + f + "_" + target;
            if (memo.containsKey(key)) return memo.get(key);

            int solutionCount = 0;
            for (int i = 1; i <= f; i++) {
                if (target < i) continue;
                solutionCount += (dfs(d - 1, f, target - i) % factor);
                solutionCount %= factor;
            }

            memo.put(key, solutionCount);
            return solutionCount;
        }
    }

    public void run() {
        Solution solution = new Solution();
        //输入：d = 1, f = 6, target = 3
        //输出：1

//        输入：d = 2, f = 6, target = 7
//        输出：6

//        输入：d = 2, f = 5, target = 10
//        输出：1

//        输入：d = 1, f = 2, target = 3
//        输出：0

//        输入：d = 30, f = 30, target = 500
//        输出：222616187
        System.out.println(solution.numRollsToTarget(30, 30, 500));
    }

    public static void main(String[] args) throws Exception {
        LC1155 an = new LC1155();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
